import { managers } from '../../engines/gameEngine';
import node_component from '../../engines/libs/node_component';
import PageBase from '../Base/PageBase';
import { SWITCH_TYPE } from '../Enum';
import { IPagePramsBase } from '../Interface';
import mainRoot from '../MainRoot';
//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html
const {ccclass, property,menu , disallowMultiple} = cc._decorator;

@ccclass
@disallowMultiple
@menu('游戏页面/暂停页面')
export default class PausePage extends PageBase{
    private switchRoot : cc.Node = null;

    async OnPageOpen(prams:IPagePramsBase){
        super.OnPageOpen(prams);
        this.switchRoot = this.content.getChildByName("SwitchRoot");
        for (let index = 0; index < this.switchRoot.children.length; index++) {
            const element = this.switchRoot.children[index];
            
            element.addComponent(SwitchBtn);
        }
    }

    OnRestartBtn(){
        managers().soundMgr.playSound('mainSound/anNiu');
        this.ClosePage();
        mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode).bundleName,this.m_mode);
    }
}

class SwitchBtn extends cc.Component{
    public noImg : cc.Node = null;
    private get open(){
        switch(this.node.name){
            case SWITCH_TYPE.SOUND:
                return managers().soundMgr.isSoundEnabled();
            case SWITCH_TYPE.VIBRATE:
                return managers().soundMgr.isVibrateEnabled();
        }
    }
    private set open(value:boolean){
        switch(this.node.name){
            case SWITCH_TYPE.SOUND:
                managers().soundMgr.setSoundEnabled(value);
                break;
            case SWITCH_TYPE.VIBRATE:
                managers().soundMgr.setVibrateEnabled(value);
                break;
        }
    }
    async onLoad(){
        this.node.on(cc.Node.EventType.TOUCH_START,this.DownFun,this);
        if(!this.noImg){
            this.noImg = new cc.Node();
            await node_component.loadResourcesPicture("Img/Disable",this.noImg);
            this.node.addChild(this.noImg);
        }
        this.noImg.active = !this.open;
    }
    public async DownFun(){
        managers().soundMgr.playSound('mainSound/anNiu');
        this.open = !this.open;
        
        this.noImg.active = !this.open;
        this.node.color = this.open?cc.color(255,255,255):cc.color(150,150,150);
        switch(this.node.name){
            case SWITCH_TYPE.SOUND:
                managers().soundMgr.setSoundEnabled(this.open);
                break;
            case SWITCH_TYPE.VIBRATE:
                managers().soundMgr.setVibrateEnabled(this.open);
                break;
        }
    }
}